package yqt.com.cnki.service.impl;


import haha.life.common.CutPageBean;
import jzqb.bo.CollectClassifyBo;
import jzqb.bo.CollectDataBo;
import jzqb.dao.CollectClassifyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yqt.com.cnki.bo.CollectionBo;
import yqt.com.cnki.bo.DownBo;
import yqt.com.cnki.bo.KnowledgeBo;
import yqt.com.cnki.bo.ViewBo;
import yqt.com.cnki.dao.IQKYDDao;
import yqt.com.cnki.dao.PersonCenterDao;
import yqt.com.cnki.po.QKYDPo;
import yqt.com.cnki.service.KnowledgeYqtService;
import yqt.com.cnki.service.PersonCenterService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PersonCenterServiceImpl implements PersonCenterService {

    @Autowired
    PersonCenterDao personCenterDao;

    @Autowired
    KnowledgeYqtService knowledgeService;

    @Autowired
    CollectClassifyDao collectClassifyDao;

    @Autowired
    IQKYDDao iqkydDao;

    @Override
    public CutPageBean<CollectionBo> getCollectionList(CollectionBo collectionBo) {
        CutPageBean<CollectionBo> cutPageBean = new CutPageBean<>();
        collectionBo.setStart((collectionBo.getPage() - 1) * collectionBo.getPageSize());
        List<CollectionBo> collectionBos = personCenterDao.getCollectionList(collectionBo);
        for (CollectionBo result : collectionBos) {
            CollectClassifyBo collectClassifyBo= collectClassifyDao.getCollectById(result.getClassifyId());
            if(null != collectClassifyBo){
                result.setClassifyName(collectClassifyBo.getCollectName());
            }
        }
        Long count = personCenterDao.getCollectionCount(collectionBo);
        cutPageBean.initCutPage(count.intValue(), collectionBo.getPageSize(), collectionBos);
        return cutPageBean;
    }

    @Override
    public Boolean insertCollection(CollectionBo collectionBo) {
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        collectionBo.setTime(now);
        Boolean bool = personCenterDao.insertCollection(collectionBo);
        return bool;



    }

    @Override
    public Boolean deleteCollection(CollectionBo collectionBo) {
        Boolean bool = personCenterDao.deleteCollection(collectionBo);
        return bool;
    }

    @Override
    public CutPageBean<DownBo> getDownList(DownBo downBo) {
        CutPageBean<DownBo> cutPageBean = new CutPageBean<>();
        downBo.setStart((downBo.getPage() - 1) * downBo.getPageSize());
        List<DownBo> downBos = personCenterDao.getDownList(downBo);
        Long count = personCenterDao.getDownListCount(downBo);
        cutPageBean.initCutPage(count.intValue(), downBo.getPageSize(), downBos);
        return cutPageBean;
    }

    @Override
    public Boolean insertDown(DownBo downBo) {
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        downBo.setTime(now);
        Boolean bool = personCenterDao.insertDown(downBo);
        return bool;
    }

    @Override
    public Boolean deleteDown(DownBo downBo) {
        Boolean bool = personCenterDao.deleteDown(downBo);
        return bool;
    }

    @Override
    public CutPageBean<ViewBo> getViewList(ViewBo viewBo) {
        CutPageBean<ViewBo> cutPageBean = new CutPageBean<>();
        viewBo.setStart((viewBo.getPage() - 1) * viewBo.getPageSize());
        List<ViewBo> viewList = personCenterDao.getViewList(viewBo);
        Long count = personCenterDao.getViewListCount(viewBo);
        cutPageBean.initCutPage(count.intValue(), viewBo.getPageSize(), viewList);
        return cutPageBean;
    }

    @Override
    public Boolean insertView(ViewBo viewBo) {
        String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        viewBo.setTime(now);
        //插入日志表
        Boolean bool = personCenterDao.insertView(viewBo);
        //更新资源表
        QKYDPo qkydPo=  new QKYDPo();
        qkydPo.setTableCode(viewBo.getDbCode());
        qkydPo.setSysId(viewBo.getResourceId());
        iqkydDao.addBrowseRate(qkydPo);
        return bool;
    }

    @Override
    public Boolean deleteView(ViewBo viewBo) {
        Boolean bool = personCenterDao.deleteView(viewBo);
        return bool;
    }

    @Override
    public Boolean updateResource(DownBo downBo) {
        KnowledgeBo knowledgeBo = knowledgeService.getBoByConditon(downBo);
        if (knowledgeBo==null){
            return false;
        }
        Integer number= knowledgeBo.getDownRate();
        number = number+1;
        knowledgeBo.setDownRate(number);
        Boolean result =  knowledgeService.updateByCondition(knowledgeBo);
        return result;
    }

    @Override
    public Boolean deleteCollectionbyId(CollectionBo collectionBo) {
        Boolean bool = personCenterDao.deleteCollectionbyId(collectionBo);
        return  bool;
    }

}
